import 'dart:isolate';

///isolate spawnUri方式
void main() {
  print("main begin");
  test1();
  newThread();
  test2();

  print("main end");
}
void test1(){
  print("test1.....");
}

void test2(){
  print("test2.....");
}

///参数的定义可以随意，参数中接收的SendPort是需要通信的发送端口
///@sendPort
void newThread(){
  print("新线程.....");
  ReceivePort receivePort = ReceivePort();
  SendPort sendPort = receivePort.sendPort;
  Isolate.spawnUri(
      Uri(path: "./isolate_spawnUri_task.dart"),
      ["msg1","msg2","msg3"],
      sendPort
  );

  //监听
  receivePort.listen((message) {
    print("主线程接收到来自子线程消息：${message}");
    switch(message[1]){
      case 0:
        //子线程正在处理初始化数据...
        print("接收到初始化消息");
        break;
      case 1:
        //子线程异步数据正在处理中..
        print("接收到处理中状态消息");
        break;
      case 2:
        //子线程数据处理完整
        print("接收到任务完成消息");
        receivePort.close();
        break;
    }
  });

}